package top.drinyeit.nongmao.view;


import android.util.Log;
import android.widget.Adapter;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by Erebus on 2016/12/19.
 */
public class SearchHistoryDao {

        private static final String TAG = "SearchDao";
        private DbManager utils;

        public SearchHistoryDao(int version) {
            DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                    .setDbName("search.db")
                    // 不设置dbDir时, 默认存储在app的私有目录.
                    .setDbVersion(version)
                    .setDbOpenListener(new DbManager.DbOpenListener() {
                        @Override
                        public void onDbOpened(DbManager db) {
                            // 开启WAL, 对写入加速提升巨大
                            db.getDatabase().enableWriteAheadLogging();
                        }
                    });
            utils = x.getDb(daoConfig);
        }



        public void save(SearchHistory searchHistory) {
            try {
             //   deleteAll();
                utils.save(searchHistory);
            } catch (DbException e) {
                Log.e(TAG, "save" + e.getMessage());
            }
        }


        public void delete(SearchHistory searchHistory) {
            try {
                utils.delete(searchHistory);
            } catch (DbException e) {
                Log.e(TAG, "delete" + e.getMessage());
                e.printStackTrace();
            }
        }


        public void update(SearchHistory searchHistory) {
            try {
                utils.update(searchHistory);
            } catch (DbException e) {
                Log.e(TAG, "update" + e.getMessage());
                e.printStackTrace();
            }
        }


        public SearchHistory  findById(Integer id) {
            return null;
        }


        public List<SearchHistory> findAll() {
            try {
                Log.d(TAG, "findAll");
                return utils.findAll(SearchHistory.class);

            } catch (DbException e) {
                Log.e(TAG, "findAll" + e.getMessage());
                e.printStackTrace();
                return null;
            }

        }

        public void deleteAll() {
            try {
                utils.delete(SearchHistory.class);
                Log.d(TAG, "deleteAll");
            } catch (DbException e) {
                Log.e(TAG, "deleteAll" + e.getMessage());
                e.printStackTrace();
            }
        }
      public void deleteAll1(Adapter m) {
        try {
            utils.delete(SearchHistory.class);
            Log.d(TAG, "deleteAll");

        } catch (DbException e) {
            Log.e(TAG, "deleteAll" + e.getMessage());
            e.printStackTrace();
        }
    }
    }